package com.seselin.stepdemo;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by Seselin on 2016/7/15.
 */
public class StepView extends View {

    public static final int STEP_DOING = 1;
    public static final int STEP_UNDO = 2;
    public static final int STEP_DONE = 3;

    private final TextPaint textPaint;
    private final Paint paint;
    private final Context context;

    public StepView(Context context) {

        this(context, null);
    }

    public StepView(Context context, AttributeSet attrs) {
        super(context, attrs);

        this.context = context;
        this.paint = new Paint();
        this.paint.setAntiAlias(true); // 消除锯齿

        this.textPaint = new TextPaint();
        this.textPaint.setAntiAlias(true); // 消除锯齿
    }

    private float size = 0.7f;
    private int stepIndex = 1;
    private int state = STEP_UNDO;

    public void setStepIndex(int stepIndex) {
        this.stepIndex = stepIndex;
    }

    public void setState(int state) {
        this.state = state;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        switch (state) {
            case STEP_DOING:
                canvas = drawDoing(canvas);
                break;
            case STEP_UNDO:
                canvas = drawUndo(canvas);
                break;
            case STEP_DONE:
                canvas = drawDone(canvas);
                break;
        }

        super.onDraw(canvas);
    }

    private Canvas drawDoing(Canvas canvas) {
        int wCenter = getWidth() / 2;
        int hCenter = getHeight() / 2;
        int innerCircle = dip2px(context, 21 * size); // 设置内圆半径
        int ringWidth = dip2px(context, 4 * size); // 设置圆环宽度
        int backWidth = dip2px(context, 3 * size); // 设置外围宽度
        int textSize = dip2px(context, 32 * size); // 设置字体宽度
        this.paint.setStyle(Paint.Style.STROKE); // 绘制空心圆

        // 绘制内圆
        this.paint.setColor(Color.WHITE);
        this.paint.setStrokeWidth(2);
        canvas.drawCircle(wCenter, hCenter, innerCircle, this.paint);

        // 绘制圆环
        this.paint.setColor(Color.GREEN);
        this.paint.setStrokeWidth(ringWidth);
        canvas.drawCircle(wCenter, hCenter, innerCircle + 1 + ringWidth / 2,
                this.paint);

        // 绘制外圆
        this.paint.setColor(Color.CIRCLE_GRAY);
        this.paint.setStrokeWidth(2);
        canvas.drawCircle(wCenter, hCenter, innerCircle + ringWidth, this.paint);

        // 绘制外围颜色尽量与背景保持一致
        this.paint.setColor(Color.CIRCLE_GRAY);
        this.paint.setStrokeWidth(backWidth);
        canvas.drawCircle(wCenter, hCenter, innerCircle + 2 + ringWidth / 2
                + backWidth / 2, this.paint);

        textPaint.setTextSize(textSize);
        textPaint.setColor(Color.GREEN);
        textPaint.setTextAlign(Paint.Align.CENTER);

        Paint.FontMetrics fontMetrics = textPaint.getFontMetrics();
        float fontHeight = fontMetrics.bottom - fontMetrics.top;
        float textBaseY = hCenter + fontHeight / 4;

        canvas.drawText("" + stepIndex, wCenter, textBaseY, textPaint);
        return canvas;
    }

    private Canvas drawUndo(Canvas canvas) {
        int wCenter = getWidth() / 2;
        int hCenter = getHeight() / 2;
        int innerCircle = dip2px(context, 24 * size); // 设置内圆半径
        int textSize = dip2px(context, 32 * size); // 设置字体宽度

        this.paint.setStyle(Paint.Style.FILL); // 绘制实心圆
        // 绘制内圆
        this.paint.setColor(Color.DISABLE_GRAY);
        this.paint.setStrokeWidth(2);
        canvas.drawCircle(wCenter, hCenter, innerCircle, this.paint);

        textPaint.setTextSize(textSize);
        textPaint.setColor(Color.WHITE);
        textPaint.setTextAlign(Paint.Align.CENTER);

        Paint.FontMetrics fontMetrics = textPaint.getFontMetrics();
        float fontHeight = fontMetrics.bottom - fontMetrics.top;
        float textBaseY = hCenter + fontHeight / 4;

        canvas.drawText("" + stepIndex, wCenter, textBaseY, textPaint);
        return canvas;
    }

    private Canvas drawDone(Canvas canvas) {
        int wCenter = getWidth() / 2;
        int hCenter = getHeight() / 2;
        int innerCircle = dip2px(context, 22 * size); // 设置内圆半径
        int textSize = dip2px(context, 32 * size); // 设置字体宽度

        this.paint.setStyle(Paint.Style.STROKE); // 绘制实心圆
        // 绘制内圆
        this.paint.setColor(Color.GREEN);
        this.paint.setStrokeWidth(7);
        canvas.drawCircle(wCenter, hCenter, innerCircle, this.paint);

        textPaint.setTextSize(textSize);
        textPaint.setColor(Color.GREEN);
        textPaint.setTextAlign(Paint.Align.CENTER);

        Paint.FontMetrics fontMetrics = textPaint.getFontMetrics();
        float fontHeight = fontMetrics.bottom - fontMetrics.top;
        float textBaseY = hCenter + fontHeight / 4;

        canvas.drawText("√", wCenter, textBaseY, textPaint);
        return canvas;
    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}
